package org.tio.sitexxx.service.service.gl;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Question;
import org.tio.sitexxx.service.vo.Const;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ConsultantControllerService {
    private static Logger log	= LoggerFactory.getLogger(ConsultantControllerService.class);
    public static final ConsultantControllerService	me	= new ConsultantControllerService();

    /**
     * 分页返回为回答的题目集合
     * @param pageNumber page
     * @param pageSize  size
     * @return  Page<Question>
     */
    public Page<Question>  questionList(int pageNumber,int pageSize){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.questionList";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        return Question.dao.paginate(pageNumber,pageSize,sqlPara);
    }

    /**
     * 分页返回为回答的题目总数
     * @return
     */
    public int count(){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.questionListCount";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid);
        return Db.use(db).queryInt(sqlPara.getSql());
    }

    /**
     * 返回顾问已回答的问题
     * @param consultantId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<Record> answeredQuestionListByConsultantId(int consultantId, int pageNumber, int pageSize){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.answeredQuestionListByConsultantId";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,consultantId);
//        System.out.println("c:"+sqlPara.getSql());
        return Db.use(db).paginate(pageNumber,pageSize,sqlPara);
    }

    /**
     *顾问已回答的问题条数
     * @param consultantId 顾问id
     * @return
     */
    public int answeredQuestionListByConsultantIdCount(int consultantId){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.count";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,consultantId);
        System.out.println("sql->>>"+JSON.toJSONString(sqlPara)+"sql:"+sqlPara.getSql());
        return Db.use(db).queryInt(sqlPara.getSql(),consultantId);
    }

    /**
     *
     * @param FDesignId
     * @return
     */
    public Map<String,Object>getDesignId(Integer FDesignId){
        String db = Const.Db.TIO_SITE_MAIN;
        String sqlid = "gl_question.selectDesignId";
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,FDesignId);
        Record record= Db.use(db).findFirst(sqlPara.getSql(),FDesignId);
        if(record!=null){
            Map<String ,Object>map=record.getColumns();
            return map;
        }
        return null;
    }
    /**
     * 查找该顾问所有的kf订单
     * @param consultantId
     * @return
     */
    public Page<Record> findPaymentsByConsultantId(Integer pageNumber, Integer pageSize , Integer consultantId){
        String select = "select a.*,b.name,b.avatar";
        String sql  = "from payment a left join member b on a.memberId = b.id where a.type = 'kf' and a.is_paid = 1 and a.consultant_member_id = ? order by a.updatedAt desc";
        return Db.use(Const.Db.TIO_SITE_MAIN).paginate(pageNumber,pageSize, select,sql,consultantId);
    }
}
